package com.aoyy.controller;

import java.util.Map;
import java.util.Date;
import javax.servlet.http.HttpServletRequest;
import org.springframework.stereotype.Controller;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.servlet.ModelAndView;
import com.aoyy.model.Post;
import com.aoyy.model.Topic;
import com.aoyy.model.User;
import com.aoyy.model.BoardManager;
import com.aoyy.service.PostService;
import com.aoyy.service.TopicService;
import com.aoyy.service.UserService;
import com.aoyy.service.BoardManagerService;
import com.aoyy.constant.Constants;
import com.aoyy.util.Page;
/**========================================================================================
 * Post controller <code>PostController</code>
 * @author this author
 * @date 2017-08-16
 * ======================================================================================== */
@Controller
public class PostController extends BaseController {
	@Autowired
	private PostService postService;
	@Autowired
	private TopicService topicService;
	@Autowired
	private BoardManagerService boardManagerService;
	@Autowired
	private UserService userService;
	
	// Display post list by topicId
	@RequestMapping(value="/post/postList-{topicId}", method={RequestMethod.GET})
	public String toPostListByTopicId(HttpServletRequest request, @RequestParam(value="pageNo", required=false) Integer pageNo, @PathVariable Integer topicId) {
	    topicService.updateTopicViewsByTopicId(topicId);
	    Topic topic = topicService.queryTopicByTopicId(topicId);
	    request.setAttribute("topic", topic);
	    pageNo = (pageNo == null) ? 1 : pageNo;
	    Page pagedPosts = postService.pagedQuery(pageNo, Constants.PAGE_SIZE, topicId);
	    request.setAttribute("pagedPosts", pagedPosts);
	    BoardManager boardManager = null;
	    User user = this.getSessionUser(request);
	    if (user != null) {
	    	boardManager = boardManagerService.queryBoardManagerByBoardIdAndUserId(topic.getBoard().getBoardId(), user.getUserId());
	    }
	    request.setAttribute("boardManager", boardManager);
	    return "/post/postList";
	}
	
	// Display post by postId
	@RequestMapping(value="/post/postDetail-{postId}", method={RequestMethod.GET})
	public ModelAndView toPostDetail(@PathVariable Integer postId) {
		ModelAndView view = new ModelAndView();
		view.setViewName("/post/postDetail");
		Post post= postService.queryPostByPostId(postId);
		view.addObject("post", post);
		return view;
	}
	
	// To add post by topicId
	@RequestMapping(value="/post/addPost-{topicId}", method={RequestMethod.GET})
	public String toAddPost(@PathVariable Integer topicId, Map<String, Object> map) {
		map.put("topicId", topicId);
		return "/post/addPost";
	}
	
	// Add post by topicId
	@RequestMapping(value="/post/addPost-{topicId}", method={RequestMethod.POST})
	public ModelAndView addPost(HttpServletRequest request, @PathVariable Integer topicId, Post post) {
		ModelAndView view = new ModelAndView();
		view.setViewName("/post/addPost");
		if (post.getTitle() == null || post.getTitle().isEmpty()) {
			view.addObject("message", "Title is required.");
			return view;
		}
		if (post.getTitle().length() > 255) {
			view.addObject("message", "Title is required and no more than 255 characters.");
			return view;
		}
		if (post.getContent() == null || post.getContent().isEmpty()) {
			view.addObject("message", "Content is required.");
			return view;
		}
		User user = this.getSessionUser(request);
		post.setUser(user);
		Topic topic = topicService.queryTopicByTopicId(topicId);
		post.setTopic(topic);
		post.setCreateTime(new Date());
	    postService.save(post);
	    userService.updateUserCreditByEmitOrRemovePost(user.getUserId(), Constants.POST_EMIT);
		view.addObject("message", "Add Post Success!");
		return view;
	}
	
	// To delete post by postId
	@RequestMapping(value="/post/deletePost-{postId}", method={RequestMethod.GET})
	public String toDeletePost(@PathVariable Integer postId, Map<String, Object> map) {
		Post post = postService.queryPostByPostId(postId);
		map.put("post", post);
		map.put("message", "Do you confirm to delete this post?");
		return "/post/deletePost";
	}
	
	// Delete post by postId
	@RequestMapping(value="/post/deletePost-{postId}", method={RequestMethod.POST})
	public ModelAndView deletePost(@PathVariable Integer postId) {
		ModelAndView view = new ModelAndView();
		view.setViewName("/post/deletePost");
		Post post = null;
		if (postId != null) {
			post = postService.queryPostByPostId(postId);
		}
		if (post == null) {
			view.addObject("message", "This post has been deleted.");
			return view;
		}
		postService.delete(post);
		userService.updateUserCreditByEmitOrRemovePost(post.getUser().getUserId(), Constants.POST_REMOVE);
		view.addObject("message", "Delete Post Success!");
		return view;
	}
	
	// To update post 
	@RequestMapping(value="/post/updatePost-{postId}", method={RequestMethod.GET})
	public String toUpdatePost(@PathVariable Integer postId, Map<String, Object> map) {
		Post post = postService.queryPostByPostId(postId);
		map.put("post", post);
		return "/post/updatePost";
	}
	
	// Update post
	@RequestMapping(value="/post/updatePost-{postId}", method={RequestMethod.POST})
	public ModelAndView updatePost(@PathVariable Integer postId, Post post) {
		ModelAndView view = new ModelAndView();
		view.setViewName("/post/updatePost");
		if (post.getTitle() == null || post.getTitle().isEmpty()) {
			view.addObject("message", "Title is required.");
			return view;
		}
		if (post.getTitle().length() > 255) {
			view.addObject("message", "Title is required and no more than 255 characters.");
			return view;
		}
		if (post.getContent() == null || post.getContent().isEmpty()) {
			view.addObject("message", "Content is required.");
			return view;
		}
		Post post2 = postService.queryPostByPostId(postId);
		post2.setTitle(post.getTitle());
		post2.setContent(post.getContent());
		postService.update(post2);
		view.addObject("message", "Update Post Success!");
		return view;
	}
	
}
